<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

//require_once "$mosConfig_absolute_path/components/com_joomleague/joomleague.js.php";?>

<?php
  function show_player_ranking_heading(&$config) {
      echo '<tr class="sectiontableheader">';
       echo '<th>Rank</th>';
       echo '<th>Player</th>';
       echo '<th>Team</th>';
       //echo '<th>Proj</th>';
       echo '<th>Goals</th>';
       echo '<th>Assists</th>';
       echo '<th>Points</th>';
      echo '</tr>';
  }

  function show_goalie_ranking_heading(&$config) {
      echo '<tr class="sectiontableheader">';
       echo '<th>Rank</th>';
       echo '<th>Player</th>';
       echo '<th>Team</th>';
       echo '<th>Games</th>';
       echo '<th>Goals Allowed</th>';
       echo '<th>Shut-outs</th>';
       echo '<th>Goal Against Avg.</th>';
      echo '</tr>';
  }
  
  function show_player_ranking_entries(&$rows, &$config) {
    global $mosConfig_live_site, $joomleague, $Itemid;
  	$rank = 1;
    $previous_points = 0;
    $previous_rank = 0;
    $classrow[0] = $config['style_class1'];
    $classrow[1] = $config['style_class2'];
    
    if (count($rows)) {
	  	foreach ($rows as $r) {
	  		$points = $r['ev1']+$r['ev2'];
	  		if ($previous_points == $points) {
	  		  $current_rank = $previous_rank;
	  		  $rk = "-";
	  		}
	  		else {
	  		  $current_rank = $rank;
	  		  $rk = $rank;
	  		  $previous_points = $points;
	  		}
	    	echo '<tr class="'.$classrow[$rank % 2].'">';
	    	 echo "<td>".$rk."</td>";
	    	 echo "<td>";
		    	 if ($config['link_to_player']) {
		    	 	echo '<a href="'
		    	 	     .sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayer&amp;p=$joomleague->id&amp;Itemid=$Itemid&amp;pid=".$r['playertoolid'])
				         .'">';
		    	  echo $r['firstname']." ".$r['lastname'];
		    	  echo '</a>';
		    	 }
		    	 else $r['firstname']." ".$r['lastname'];
	    	 echo "</td>";
	  	   echo "<td>";
		    	 if ($config['link_to_team']) {
		         echo '<a href="'
		               .sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayers&amp;p=$joomleague->id&amp;Itemid=$Itemid&amp;ttid=".$r['teamtoolid'])
		               .'">';
		         echo $r['team_name'];
		         echo '</a>';
		       }
		       else $r['team_name'];
	       echo "</td>";
	       //echo "<td>".$r['project_id']."</td>";
	    	 echo "<td>".($r['ev1']>0?$r['ev1']:"-")."</td>";
	       echo "<td>".($r['ev2']>0?$r['ev2']:"-")."</td>";
	       echo "<td>".$points."</td>";
	      echo "</tr>";
	      $rank++;
	    }
    }
  }

  
  function show_goalie_ranking_entries(&$rows, &$config) {
    global $mosConfig_live_site, $joomleague, $Itemid;
    $rank = 1;
    $previous_avg = -1;
    $previous_rank = 0;
    $classrow[0] = $config['style_class1'];
    $classrow[1] = $config['style_class2'];
    
    if (count($rows)) {
      foreach ($rows as $r) {
        $goalAgainstAvg = $r['goals_allowed']/$r['games'];
        if ($previous_avg == $goalAgainstAvg) {
          $current_rank = $previous_rank;
          $rk = "-";
        }
        else {
          $current_rank = $rank;
          $rk = $rank;
          $previous_avg = $goalAgainstAvg;
        }
        echo '<tr class="'.$classrow[$rank % 2].'">';
         echo "<td>".$rk."</td>";
         echo "<td>";
           if ($config['link_to_player']) {
            echo '<a href="'
                 .sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayer&amp;p=$joomleague->id&amp;Itemid=$Itemid&amp;pid=".$r['playertoolid'])
                 .'">';
            echo $r['firstname']." ".$r['lastname'];
            echo '</a>';
           }
           else $r['firstname']." ".$r['lastname'];
         echo "</td>";
         echo "<td>";
           if ($config['link_to_team']) {
             echo '<a href="'
                   .sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayers&amp;p=$joomleague->id&amp;Itemid=$Itemid&amp;ttid=".$r['teamtoolid'])
                   .'">';
             echo $r['team_name'];
             echo '</a>';
           }
           else $r['team_name'];
         echo "</td>";
         echo "<td>".$r['games']."</td>";
         echo "<td>".$r['goals_allowed']."</td>";
         echo "<td>".$r['shutouts']."</td>";
         echo "<td>".round($goalAgainstAvg,3)."</td>";
        echo "</tr>";
        $rank++;
        
//       echo '<th>Goals Allowed</th>';
//       echo '<th>Shut-outs</th>';
//       echo '<th>Goal Against Avg.</th>';
      }
    }
  }
  
  ?>

<a name="jl_top" id="jl_top"></a>
<!-- Header with Project Name-->
<?php 
if ($playerTable_config['show_project_heading'] == "1" OR $overall_config['show_project_heading'] == "1") { 
  ?>
  <div class="componentheading"<?php if ($playerTable_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo ' style="float:left;"';?>>
  <?php echo HTML_joomleague::showTitle($project, $overall_config['project_heading_type']); ?>
  </div>
  <!-- Druckfunktion -->
    <?php 
    if ($playerTable_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") {
      HTML_joomleague::PrintIcon( $row, $params, false, '' );
    }
    ?>
  <!-- Druckfunktion Ende -->
  <?php 
} 
?>
<!-- Header END -->


<!-- Player ranking -->
<table class="contentpaneopen" width="100%">
 	<tr>
   <td class="contentheading">
       <?php //if ($playerTable_config['show_section_heading']==1)	HTML_joomleague::show_players_ranking_title($playerTable_config);?>
      Players Ranking
   </td>
 </tr>
</table>
<!-- section header e.g. ranking, results etc. END -->

<br />

<!-- content -->
<?php

foreach ( $tables as $key=>$rows ) {
  if ($key) {
    echo '<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
    			<tr><td class="contentheading">';
      HTML_joomleague::show_ranking_division_title($key);
    echo '</td></tr></table>';
  }
  ?>
  <div id="jlg_playerRankingtable">
    <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
    <?php
    show_player_ranking_heading($playerTable_config);
    show_player_ranking_entries($rows, $playerTable_config);
    ?>
    </table>
    <?php 
    if ($limit) {
      ?>
    	<div id="jlg_linkMore">
      	<a href="<?php
        echo sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayersranking&amp;p=$project->id&amp;Itemid=$Itemid&amp;div=$key&amp;display=1");
        ?>">more...</a>
      </div>
	    <?php
	  }
	  ?>
  </div>
  <br/>
  <?php
}
?>

<!-- player ranking END -->

<!-- Goalies ranking -->
<table class="contentpaneopen" width="100%">
  <tr>
   <td class="contentheading">
       <?php //if ($playerTable_config['show_section_heading']==1)  HTML_joomleague::show_players_ranking_title($playerTable_config);?>
      Goalies Ranking
   </td>
 </tr>
</table>
<!-- section header e.g. ranking, results etc. END -->

<br />

<!-- content -->
<?php

foreach ( $tables_goalies as $key=>$rows ) {
  if ($key) {
    echo '<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
          <tr><td class="contentheading">';
      HTML_joomleague::show_ranking_division_title($key);
    echo '</td></tr></table>';
  }
  ?>
  <div id="jlg_playerRankingtable">
    <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
    <?php
    show_goalie_ranking_heading($playerTable_config);
    show_goalie_ranking_entries($rows, $playerTable_config);
    ?>
    </table>
    <?php 
    if ($limit) {
      ?>
      <div id="jlg_linkMore">
        <a href="<?php
        echo sefRelToAbs("$mosConfig_live_site/index.php?option=com_joomleague&amp;func=showplayersranking&amp;p=$project->id&amp;Itemid=$Itemid&amp;div=$key&amp;display=1");
        ?>">more...</a>
      </div>
      <?php
    }
    ?>
  </div>
  <br/>
  <?php
}
?>

<!-- ranking END -->

<!-- Print Button START -->
<?php if ($playerTable_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($playerTable_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
?></div> 
<?php };?>
<!-- Print Button END -->
